/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package swym.server.config;

import java.io.*;
import java.util.*;

/**
 *
 * @author mschaefer
 */
public class Settings 
{
    private final static String HOME_DIR = ".sync-server";
    
    /**
     * Returns the server's "home directory", with the ending
     * file separator appended.
     * @return
     */
    public static String getBasePath() 
    { 
        String path = System.getProperty( "user.home" ) + File.separator + HOME_DIR + File.separator;
        
        File folder = new File( path );
        if ( !folder.exists() )
            folder.mkdir();
        
        return path;
    }
    
    private static final Object propertySyncObject = new Object();
    private static HashMap<String, String> properties  = new HashMap<String, String>();
    private static final String CONFIG_FILE_NAME = "config";
    private static final char SEPARATOR  = ':';
    
    private static final String VALUE_YES = "yes";
    private static final String SETTING_DEBUG_MODE = "debug";

    /**
     * Private constructor - no instantiation!
     */
    private Settings()
    {}
    
    public static boolean isDebugMode() 
    {
        return getProperty( SETTING_DEBUG_MODE ).equals( VALUE_YES ); 
    }

    /**
     * Retrieves a property value in the settings map. This is a synchronized
     * wrapper for the <code>HashMap.get()</code> method.
     * @param name the name of the setting property to retrieve
     * @return the corresponding setting value
     */
    public static String getProperty( String name )
    {
        synchronized( propertySyncObject )
        {
            String strValue = properties.get( name );

            if ( strValue == null ) strValue = "";

            return strValue;
        }
    }

    /**
     * Sets a property value in the settings map. This is a synchronized
     * wrapper for the <code>HashMap.put()</code> method.
     * @param name the name of the setting property to set
     * @param value the value of the property corresponding to <code>name</code>
     */
    public static void setProperty( String name, String value )
    {
        synchronized( propertySyncObject )
        {
            properties.put( name, value );
        }
    }

    /**
     * Attempts to load the settings stored in the default settings file. This is
     * synchronized across all threads. It will fail silently, by the way.
     */
    public static void loadSettings()
    {
        synchronized (propertySyncObject)
        {
            FileReader fileReader = null;
            BufferedReader bufferedReader = null;
            
            try
            {
                fileReader = new FileReader( Settings.getBasePath() +
                                             CONFIG_FILE_NAME);
                bufferedReader = new BufferedReader(fileReader);

                String line = bufferedReader.readLine();

                while (line != null)
                {
                    int iSeparatorIndex = line.indexOf(SEPARATOR);

                    if (iSeparatorIndex > 0)
                    {
                        String name = line.substring(0, iSeparatorIndex).trim();
                        String value = line.substring(iSeparatorIndex + 1).trim();
                        properties.put(name, value);
                    }
                    line = bufferedReader.readLine();
                }
            } 
            catch (FileNotFoundException e) {}
            catch (IOException e) {}
            finally
            {
                try
                {
                    if (bufferedReader != null)
                    {
                        bufferedReader.close();
                    }
                } 
                catch (IOException e) {}
            }
        }
    }

    /**
     * Saves the settings/properties map to the default config file from which
     * <code>loadSettings()</code> loads data. This, too, will fail silently.
     */
    public static void writeSettings()
    {
        synchronized (propertySyncObject)
        {
            FileOutputStream fileOutputStream = null;
            PrintStream filePrintStream = null;

            try
            {
                fileOutputStream = new FileOutputStream( Settings.getBasePath() +
                                                         CONFIG_FILE_NAME);
                filePrintStream = new PrintStream( fileOutputStream );
                Iterator<String> keys = properties.keySet().iterator();
                String separator = Character.toString( SEPARATOR );

                while (keys.hasNext())
                {
                    String name = keys.next();
                    filePrintStream.println(name +
                            separator +
                            " " +
                            properties.get(name));
                }
            } 
            catch (FileNotFoundException e) {}
            finally
            {
                if (filePrintStream != null)
                {
                    filePrintStream.close();
                }
            }
        }
    }
}
